# 2021-8-27 10:25 merge by grx
import datetime

from rest_framework.decorators import api_view
from questionnaire.models import *
from django.http import FileResponse
from questionnaire.models import Questionnaire
from rest_framework.response import Response
from rest_framework import status
from xhtml2pdf import pisa
import markdown


@api_view(['POST', ])
def preview(request):
    wjId = request.data.get('questionnaire_id')
    response = {'code': 0, 'msg': 'success'}
    questionnaire = Questionnaire.objects.get(id=wjId)
    response['title'] = questionnaire.title
    response['desc'] = questionnaire.desc
    response['status'] = questionnaire.status
    response['username'] = questionnaire.username
    response['created_time'] = questionnaire.created_time
    response['is_remind'] = questionnaire.is_remind
    response['show_number'] = questionnaire.show_number
    response['is_out_of_order'] = questionnaire.is_out_of_order
    response['type'] = questionnaire.type
    response['limit'] = questionnaire.limit
    obj = Question.objects.filter(wjId=wjId)
    total = obj.count()
    questions = []
    for item in obj:
        temp = {'id': item.id, 'title': item.title, 'type': item.type, 'row': item.row, 'must': item.must,
                'remark': item.remark, 'options': [], 'detail': [], 'score': item.score, 'is_positioning': item.is_positioning}
        # 获取选项
        if temp['type'] in ['single', 'multi']:  # 如果是单选或者多选
            optionItems = Options.objects.filter(questionId=item.id)
            for optionItem in optionItems:
                temp['options'].append({'title': optionItem.title, 'id': optionItem.id, 'limit': optionItem.limit,
                                        'remain': optionItem.remain, 'is_correct': optionItem.is_correct})
        elif temp['type'] == 'grade':
            # if
            gradeItem = GradeQuestionAttribute.objects.get(questionId=item.id)
            temp['detail'] = {
                'grade_type': gradeItem.grade_type,
                'degree_min': gradeItem.degree_min,
                'degree_max': gradeItem.degree_max,
                'range': gradeItem.range,
                'start': gradeItem.start,
            }
        elif temp['type'] == 'fill':
            temp['correct_answer'] = item.correct_answer
        questions.append(temp)
    response['total'] = total
    response['questions'] = questions
    if questionnaire.is_set_date:
        response['end_time'] = questionnaire.end_time + datetime.timedelta(hours=8)
    else:
        response['end_time'] = None
    return Response(response, status=status.HTTP_200_OK)


@api_view(['POST', ])
def previewWithoutAnswer(request):
    questionnaire_id = request.data.get('questionnaire_id')
    if questionnaire_id is None:
        return Response({'msg': 'id is null', 'code': -1}, status=status.HTTP_400_BAD_REQUEST)
    if Questionnaire.objects.filter(id=questionnaire_id).count() == 0:
        return Response({'msg': 'can not find questionnaire', 'code': -1}, status=status.HTTP_400_BAD_REQUEST)
    questionnaire = Questionnaire.objects.get(id=questionnaire_id)
    questions_object = Question.objects.filter(wjId=questionnaire_id)
    questions = []
    for question_object in questions_object:
        question = {'title': question_object.title,
                    'type': question_object.type,
                    'row': question_object.row,
                    'must': question_object.must,
                    'remark': question_object.remark,
                    'score': question_object.score,
                    'is_positioning': question_object.is_positioning
                    }
        if question_object.type == 'multi' or question_object.type == 'single':
            options = []
            options_object = Options.objects.filter(questionId=question_object.id)
            for option_object in options_object:
                option = {'title': option_object.title, 'id': option_object.id, 'limit': option_object.limit, 'remain': option_object.remain}
                options.append(option)
            question['options'] = options
        elif question_object.type == 'grade':
            g = GradeQuestionAttribute.objects.get(questionId=question_object.id)
            detail = {'grade_type': g.grade_type, 'degree_min': g.degree_min, 'degree_max': g.degree_max, 'range': g.range, 'start': g.start}
            question['detail'] = detail
        questions.append(question)
    response = {'questionnaire_id': questionnaire.id,
                'title': questionnaire.title,
                'desc': questionnaire.desc,
                'status': questionnaire.status,
                'username': questionnaire.username,
                'total': questions_object.count(),
                'questions': questions
                }
    if questionnaire.is_set_date:
        response['end_time'] = questionnaire.end_time + datetime.timedelta(hours=8)
    else:
        response['end_time'] = None
    return Response(response, status=status.HTTP_200_OK)





